<?php
/*
 * Aclass_nav 导航类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com


$this->order		排序
$this->page			页数
$this->if_order		
$this->if_by		
$this->if_page		
$this->other		附加


 */
class Aclass_page{
	function  __construct($arr) {
		if($arr->order){
			$arroby = explode(' ',$arr->order);
			if(count($arroby)==2){
				$arr->by = $arroby[0];
				$arr->order = $arroby[1];
			}
		}
		$this->theurl = $arr->theurl;
		$this->counts = $arr->counts;
		$this->other = $arr->other;
		$this->page = $arr->page;
		$aorder = explode(',',$_GET['aorder']);
		$apage = explode(',',$_GET['apage']);
		$this->by = $aorder[0];
		$this->start = $apage[0];
		if($_GET['aorder']){
			$this->aorder = '-aorder-'.$_GET['aorder'];
		}
		if(empty($this->start)){
			$this->start = '1';
		}
		if(empty($this->by)){
			$this->by = $arr->by;
		}
		if(empty($this->order)){
			if($aorder[1]){
				$this->order = $aorder[1];
			}elseif($arr->order){
				$this->order = $arr->order;
			}else{
				$this->order = 'ASC';
			}
		}
		if($apage[1]){
			$this->page = $apage[1];
			$this->if_page = '1';
		}elseif(empty($this->page)){
			$this->page = '12';
		}
		if($this->order == 'ASC'){
			$this->neworder = 'DESC';
			$this->ordermg = 1;
		}else{
			$this->neworder = 'ASC';
			$this->ordermg = -1;
		}
		$this->ordermg = array($this->by=>$this->ordermg);
		$this->orderby = $this->by.' '.$this->order;
		$this->limit = ($this->start - 1)*$this->page.','.$this->page;
		if(empty($this->other)){
			$URIrr = explode('?',$_SERVER["REQUEST_URI"]);
			if($URIrr[1]){
				$this->other = '?'.$URIrr[1];
			}
		}
	}
	function apage(){
		if($this->counts>$this->page){
			if($this->if_by=='1'){
				if($this->if_order=='1'){
					$and="-aorder-".$this->by.','.$this->neworder.$this->aorder.$this->other;
				}else{
					$and="-aorder-".$this->by.$this->aorder.$this->other;
				}
			}else{
				$and=$this->aorder.$this->other;
			}
			if($this->if_page == '1'){
				$andpage = ','.$this->page;
			}else{
				$andpage = '';
			}
			$pagenum = floor(($this->counts-1)/$this->page);
			$pageup=$this->start - 1;
			$pagedown=$this->start + 1;
			$pageend=$pagenum + 1;
		$apagehtml = '<style>.apage{width:auto; margin:0 auto;}
		.apage a{margin:0 3px 0 0;padding:2px 5px;color:#008000;background:none repeat scroll 0 0 #FCF9E6;border:1px solid #EBE6C9;text-decoration:none;}
		</style><div class="apage">';
			if($pagenum<9){
				$apagehtml .= "<a>".$this->counts."</a>";
				if($this->start>1){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.$pageup.$andpage.$and.'"><-</a>';
				}else{
					$apagehtml .= '<a><-</a>';
				}
				$i=0;
				for($i;$i<$pagenum+1;$i++){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.($i+1).$andpage.$and.'">';
					if($this->start==$i+1){$apagehtml .= '<b>';}
					$apagehtml .= $i+1;
					if($this->start==$i+1){$apagehtml .= '</b>';}
						$apagehtml .= '</a>';
				}
				if($this->start<$pageend){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.$pagedown.$andpage.$and.'">-></a>';
				}else{
					$apagehtml .= "<a>-></a>";
				}
			}else if($pagenum>8){
				$apagehtml .= "<a>".$this->counts."</a>";
				if($this->start>=5){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-1'.$andpage.$and.'">1 ...</a>';
				}
				if($this->start>1){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.$pageup.$andpage.$and.'"><-</a>';
				}else{
					$apagehtml .= '<a><-</a>';
				}
				if($this->start<=4){
					$i=0;
					$iplus=$i+3;
					for($i;$i-2<$iplus;$i++){
						$apagehtml .= '<a href="'.$this->theurl.'-apage-'.($i+1).$andpage.$and.'">';
						if($this->start==$i+1){$apagehtml .= '<b>';}
							$apagehtml .= $i+1;
						if($this->start==$i+1){$apagehtml .= '</b>';}
						$apagehtml .= '</a>';
					}
				}else if($this->start>=5 && $this->start<($pagenum-2)){
					$i=$this->start-3;
					$iplus=$i+3;
					for($i;$i-2<$iplus;$i++){
						$apagehtml .= '<a href="'.$this->theurl.'-apage-'.($i+1).$andpage.$and.'">';
						if($this->start==$i+1){$apagehtml .= '<b>';}
						$apagehtml .= $i+1;
						if($this->start==$i+1){$apagehtml .= '</b>';}
						$apagehtml .= '</a>';
					}
				}else if($this->start>=($pagenum-2)){
					$i=$pagenum-4;
					$iplus=$pagenum-1;
					for($i;$i-2<$iplus;$i++){
						$apagehtml .= '<a href="'.$this->theurl.'-apage-'.($i+1).$andpage.$and.'">';
						if($this->start==$i+1){$apagehtml .= '<b>';}
						$apagehtml .= $i+1;
						if($this->start==$i+1){$apagehtml .= '</b>';}
						$apagehtml .= '</a>';
					}
				}
				if($this->start<$pageend){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.$pagedown.$andpage.$and.'">-></a>';
				}else{
					$apagehtml .= "<a>-></a>";
				}
				if($pagenum>6 && $this->start<($pagenum-2)){
					$apagehtml .= '<a href="'.$this->theurl.'-apage-'.$pageend.$andpage.$and.'">... '.$pageend.'</a>';
				}
		//	$apagehtml .= "<form action='' method='get'><input type='text' name='go' size='3' value=''>
		//		<input type='submit' value='跳转'></form>";
		//	$apagehtml .= "<input type='text' name='start' size='3' value=''>
		//		<input type='hidden' name='by' value='$by'>
		//		<input type='hidden' name='order' value='$order'>
		//		<input type='submit' value='GO'></form>";
			}
			$apagehtml .= '</div>';
		}
		$this->html = $apagehtml;
		return $this;
	}
	function sorturl($by){
		return $this->theurl.'-aorder-'.$by.','.$this->neworder.$this->other;
	}

}

?>
